home *** CD-ROM | disk | FTP | other *** search
- {I have developed a general purpose sort routine quite some time ago
- and put irt in one of my standard library units. The implementation is
- actually quite simple, becuase the application has to provide code for
- comparing and swapping the various elements. The only requirement is
- thta the elements must be available via an index (like an array or a
- random access file).
- I have included the code and a short description of the routine.
-
- The sort is implemented as heapsort, because this sorting algorithm
- does not need significant memory space (as quicksort needs,
- particularly in case of a perfect ordered set!). The efficiency of
- this algorithm is quite good, actually outperfing quicksort in many
- reallife situations, where the data are already partly sorted..
- }
-
- Type
- USSortLessType=Function(Index1: LongInt; Index2: LongInt):
- Boolean;
- USSortSwapType=Procedure(Index1: LongInt; Index2: LongInt);
-
-
- Function USSort(
- IndexLow: LongInt;
- IndexHigh: LongInt;
- USSortLess: USSortLessType;
- USSortSwap: USSortSwapType): Byte;
-
- Label
- 99;
-
- Var
- L: LongInt;
- I: LongInt;
- J: LongInt;
- Length: LongInt;
- IR: LongInt;
- IRRA: LongInt;
-
- Begin
- Length:=IndexHigh-IndexLow+1;
- If Length>1 Then
- Begin
- L:=(Length Div 2)+1;
- IR:=Length;
-
- While TRUE Do
- Begin
- If L>1 Then
- Begin
- Dec(L);
- IRRA:=L;
- End
- Else
- Begin
- USSortSwap(IR+IndexLow-1,1+IndexLow-1);
- IRRA:=1;
- Dec(IR);
- If IR=1 Then
- Begin
- Goto 99;
- End;
- End;
- I:=L;
- J:=L+L;
- While J<=IR Do
- Begin
- If J<IR Then
- Begin
- If USSortLess(J+IndexLow-1,J+1+IndexLow-1) Then
- Begin
- Inc(J);
- End;
- End;
- If USSortLess(IRRA+IndexLow-1,J+IndexLow-1) Then
- Begin
- USSortSwap(I+IndexLow-1,J+IndexLow-1);
- IRRA:=J;
- I:=J;
- J:=J+J;
- End
- Else
- Begin
- J:=IR+1;
- End;
- End;
- End;
- 99:
- End;
-
- USSort:=0;
- End; {USSort}
-
- {
- *START DESCRIPTION*
- The functione USSort may be used to sort a set of elements. The
- routine
- is transparent to the type of information in the set.
- The elements in the set are are indexed and the lower and upper
- indexed
- are given by the parameters IndexLow and IndexHigh.
- The calling program should provide a function USSortLess, which
- must return TRUE if the element indexed by the first parameter is
- less than the element indexed by the second parameter.
- Furthermore, a procedure USSortSwap must be provided. This
- routine must swap the elements indexed by the two parameters.
-
- The function returns with 0 if the sort completed successfully.
- Otherwise,
- the error code 1 is returned, indicating insufficient memory to sort
- the set.
-
-
- Example
-
- Var
- Table: Array[1..100] Of Real;
-
- Function SortLess(Index1: LongInt; Index2: LongInt): Boolean; Far;
- Begin
- SortLess:=Table[Index1]<<Table[Index2];
- End;
-
- Procedure SortSwap(Index1: LongInt; Index2: LongInt); Far;
- Var
- Temp: Real;
- Begin
- Temp:=Table[Index1];
- Table[Index1]:=Table[Index2];
- Table[Index2]:=Temp;
- End;
-
- Begin
- {Fill Table}
- Result:=USSort(1,100,SortLess,SortSwap);
-
- In this example, the table with 100 reals will be sorted.
-
- Please let me know if you need further assistance (via the Newsgroup
- of email rvdham@ect.nl)
-
- }
-
-
-